<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Adminlog Model
 *
 */
class Userlog_Model extends HYD_Model {
    private $_sql_where = '';
    public function __construct() {
        parent::__construct();
        $this->set_table_name('userlog');
    }

    /**
     * 保存操作日志数据包括插入、更新
     *
     * 更新或插入日志数据,$id为NULL时插入，反之更新
     * @param	string id 日志记录ID
     * @param	array $data 数据
     * @return	integer  1=成功   MODEL_RETURN_DBERROR=数据库错误
     */
    public function save($data, $id = NULL) {

        //事务开始
        $this->db->trans_begin();
        $row_data = $this->get_update_feilds($data);

        if (is_numeric($id)) {
            $this->db->update($this->get_table_name(), $row_data, array('fd_id' => $id));
        } else {
            $this->db->insert($this->get_table_name(), $row_data);
        }
        if ($this->check_dbtrans() === FALSE) {
            return MODEL_RETURN_DBERROR;
        }
        //事务结束
        $this->db->trans_commit();
        return MODEL_RETURN_SUCCESS;
    }

    /**
     * 删除操作日志数据记录
     *
     * 删除指定一条操作日志数据记录
     * @param	 string id 操作日志ID
     * @return	 bool  TRUE=成功 -500=失败
     */
    public function delete($organization_id = -1, $id) {
        $this->db->trans_begin();
        $where = array('fd_id' => $id);
        if ($organization_id > 0) {
            $where['fd_organizationid'] = $organization_id;
        }
        $this->db->delete($this->get_table_name(), $where);
        if ($this->check_dbtrans() === FALSE) {
            return MODEL_RETURN_DBERROR;
        }
        //事务结束
        $this->db->trans_commit();        
        return MODEL_RETURN_SUCCESS;
    }

    /**
     * 获取操作日志数据列表
     *
     * 根据分页条件获取操作日志数据列表
     * @param	integer $organization_id 组织机构ID >=0 使用此条件
     * @param	string $start 记录开始位置
     * @param	string $limit 记录数
     * @return	 array 结果集
     */
    public function get_lists($organization_id = -1, $start = NULL, $limit = NULL, $sql_str = '') {
        $feild_list = $this->get_all_feilds();
        $this->db->select($feild_list);
        $this->db->from($this->get_table_name());
        $where_str = '';
        if ($organization_id > 0) {
            $where_str = "fd_organizationid='" . $this->db->escape_str($organization_id) . "'";
        }
        //搜索处理      
        if (strlen($sql_str) > 0) {
            if ($where_str) {
                $where_str .= " and ( {$sql_str} )";
            } else {
                $where_str = $sql_str;
            }
        }

        if ($where_str) {
            $this->db->where($where_str, NULL, FALSE);
            $this->_sql_where = $where_str;
        }
        $this->db->order_by('fd_organizationid');
        $this->db->order_by('fd_datetime desc');
        //return $this->db->count_all_results();符合条件的结果
        if (!(is_null($limit) && is_null($start))) {
            $this->db->limit($limit, $start);
        }       
        $result_list = $this->db->get();
        return $result_list->result_array();
    }

    /**
     * 获取日志记录
     *
     * 根据ID获取指定一条日志记录
     * @param	 string id 权限ID
     * @param	 string organization_id 机构ID     
     * @return	 array 结果集
     */
    public function get_data($organization_id = -1, $id) {
        $feild_list = $this->get_all_feilds();
        $where['fd_id'] = $id;
        if ($organization_id > 0) {
            $where['fd_organizationid'] = $organization_id;
        }
        $result_data = $this->db
                ->select($feild_list)
                ->from($this->get_table_name())
                ->where($where)
                ->get();
        return $result_data->row_array();
    }

    /**
     * 获取记录总数
     * @return	 integer 
     */
    public function get_total($organization_id = -1) {
        if ($organization_id >= 0) {// 判断关系时用 检查一致性
            $this->db->from($this->get_table_name());
            $this->db->where('fd_organizationid', $organization_id);
            return $this->db->count_all_results();
        }    
        if($this->_sql_where)$this->db->where($this->_sql_where, NULL, FALSE);
        return $this->db->count_all_results($this->get_table_name());
        return $this->db->count_all();
    }

}

/* End of file adminlog_model.php */
/* Location: ./pbxadmin/models/adminlog/adminlog_model.php */